package com.yzy.baselibrary.utils

import android.annotation.SuppressLint
import android.app.Activity
import com.yzy.baselibrary.utils.rom.ROM
import com.yzy.baselibrary.utils.rom.RomIdentifier
import java.lang.reflect.InvocationTargetException


/**
 *description: 刘海屏工具类.
 *@date 2019/1/7 10:07.
 *@author: YinZeYu.
 */
object NotchScreenUtil {

  /**
   * 是否为刘海屏
   */
  fun isNotchScreen(activity: Activity): Boolean {
    //先用AndroidP判断
    var hasNotch = false
    hasNotch = isAndroidPNotchScreen(activity)
    if (!hasNotch) {
      //原生判断不了再根据相应的机型判断
      val romType = RomIdentifier.getRomType(activity)
      when (romType) {
        ROM.EMUI // 华为
        -> {
          hasNotch = isHuaweiNotchScreen(activity)
        }

        ROM.MIUI // 小米
        -> {
          hasNotch = isXiaomiNotchScreen(activity)
        }
        ROM.ColorOS // OPPO
        -> {
          hasNotch = isOppoNotchScreen(activity)
        }
        ROM.FuntouchOS //Vivo
        -> {
          hasNotch = isVivoNotchScreen(activity)
        }
        else -> {//其他都走原生
          hasNotch = isAndroidPNotchScreen(activity)
        }
      }
    }
    return hasNotch
  }


  /**
   * Android 原生的刘海屏判断
   */
  private fun isAndroidPNotchScreen(activity: Activity): Boolean {
    val decorView = activity.window.decorView
    if (decorView != null && android.os.Build.VERSION.SDK_INT >= 28) {
      val windowInsets = decorView.rootWindowInsets
      if (windowInsets != null)
        return true
    }
    return false
  }

  /**
   * 小米刘海屏判断
   */
  private fun isXiaomiNotchScreen(activity: Activity): Boolean {
    var result = 0
    try {
      val classLoader = activity.classLoader
      val SystemProperties = classLoader.loadClass("android.os.SystemProperties")
      //参数类型
      val paramTypes = arrayOfNulls<Class<*>>(2)
      paramTypes[0] = String::class.java
      paramTypes[1] = Int::class.javaPrimitiveType
      val getInt = SystemProperties.getMethod("getInt", *paramTypes)
      //参数
      val params = arrayOfNulls<Any>(2)
      params[0] = "ro.miui.notch"
      params[1] = 0
      result = getInt.invoke(SystemProperties, params) as Int

    } catch (e: ClassNotFoundException) {
      e.printStackTrace()
    } catch (e: NoSuchMethodException) {
      e.printStackTrace()
    } catch (e: IllegalAccessException) {
      e.printStackTrace()
    } catch (e: IllegalArgumentException) {
      e.printStackTrace()
    } catch (e: InvocationTargetException) {
      e.printStackTrace()
    }
    return result == 1
  }

  /**
   * 华为刘海屏的判断
   */
  private fun isHuaweiNotchScreen(activity: Activity): Boolean {
    var ret = false
    try {
      val classLoader = activity.classLoader
      val HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil")
      val get = HwNotchSizeUtil.getMethod("hasNotchInScreen")
      ret = get.invoke(HwNotchSizeUtil) as Boolean
    } catch (e: ClassNotFoundException) {
    } catch (e: NoSuchMethodException) {
    } catch (e: Exception) {
    } finally {
      return ret
    }
  }

  /**
   * vivo刘海屏判断
   */
  @SuppressLint("PrivateApi")
  private fun isVivoNotchScreen(activity: Activity): Boolean {
    val VIVO_NOTCH = 0x00000020
    var ret = false
    try {
      val classLoader = activity.classLoader
      val FtFeature = classLoader.loadClass("android.util.FtFeature")
      val method = FtFeature.getMethod("isFeatureSupport", Int::class.javaPrimitiveType)
      ret = method.invoke(FtFeature, VIVO_NOTCH) as Boolean
    } catch (e: ClassNotFoundException) {
    } catch (e: NoSuchMethodException) {
    } catch (e: Exception) {
    } finally {
      return ret
    }
  }

  /**
   * oppo刘海屏判断
   */
  private fun isOppoNotchScreen(activity: Activity): Boolean {
    return activity.packageManager.hasSystemFeature("com.oppo.feature.screen.heteromorphism");
  }
}